package com.chushouya.controller.order.api;

import com.chushouya.common.constant.ServiceType;
import com.chushouya.order.dto.api.order.*;
import com.chushouya.order.service.common.SuhuishouSyncService;
import com.general.framework.core.schema.PageResult;
import com.general.framework.core.schema.Response;
import com.general.framework.web.annotations.RepeatSubmit;
import com.chushouya.order.dto.admin.order.OrderListApiDTO;
import com.chushouya.order.dto.admin.order.UserOrderAction;
import com.chushouya.order.service.api.OrderApiService;
import com.chushouya.order.service.api.OrderSubmitApiService;
import com.chushouya.order.service.api.UserOrderActionCtrlService;
import com.chushouya.order.service.api.UserOrderActionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.List;

@Api(tags = ServiceType.API + "订单接口")
@RestController
@RequestMapping(ServiceType.API + "/order")
@Slf4j
public class OrderApiController {
    @Resource
    private OrderApiService orderApiService;

    @Resource
    private OrderSubmitApiService orderSubmitApiService;

    @Resource
    private UserOrderActionCtrlService userOrderActionCtrlService;

    @Resource
    private UserOrderActionService userOrderActionService;

    @Resource
    private SuhuishouSyncService suhuishouSyncService;

    @ApiOperation("创建订单接口")
    @PostMapping("/submit")
    @RepeatSubmit
    public Response<OrderUserApiDTO> orderCreate(@RequestBody @Validated OrderSubmitApiDTO data) {
        return Response.success(orderSubmitApiService.submitOrder(data));
    }

    @ApiOperation("订单列表接口")
    @GetMapping("/search")
    public Response<PageResult<OrderListApiDTO>> orderList(OrderApiQuery query) {
        return Response.success(orderApiService.userOrderSearch(query));
    }

    @ApiOperation("订单详情接口")
    @GetMapping("/detail")
    public Response<OrderInfoApiDTO> orderDetail(String orderNo) {
        return Response.success(orderApiService.userOrderDetail(orderNo));
    }

    @ApiOperation("取消订单接口")
    @PostMapping("/cancel")
    public Response orderCancel(@RequestBody @Valid OrderCancelApiDTO orderCancel) {
        userOrderActionService.cancelOrder(orderCancel);
        return Response.success("取消成功");
    }

    @ApiOperation("用户订单操作接口")
    @GetMapping("/action")
    public Response<List<UserOrderAction>> orderAction(String orderNo) {
        return Response.success(userOrderActionCtrlService.orderActions(orderNo));
    }

    @ApiOperation("更新合作商接口")
    @PostMapping("/updatePartner")
    public Response updatePartner(@RequestBody @Valid OrderUpdatePartnerApiDTO data) {
        orderApiService.updatePartner(data);
        return Response.success("更新成功");
    }

    @ApiOperation("是否可以同步速回收接口")
    @GetMapping("/canSyncSuhuishou")
    public Response<Boolean> canSyncSuhuishou(SuhuishouSyncCheckApiDTO suhuishouSyncCheckApiDTO) {
        final BigDecimal orderPrice = suhuishouSyncCheckApiDTO.getOrderPrice();
        final String userPhone = suhuishouSyncCheckApiDTO.getUserPhone();
        final Long cityCode = suhuishouSyncCheckApiDTO.getCityCode();
        final Integer orderType = suhuishouSyncCheckApiDTO.getOrderType();
        return Response.success(suhuishouSyncService.canSyncToSuhuishou(orderType, orderPrice, userPhone, cityCode));
    }
}
